#include <stdio.h>
#include <Elementary.h>
#include <libcal-character.h>
#include <libui-char-status.h>
#include <libabs-appfwk.h>
#include <Edje.h>
#include <Evas.h>
#include <Ecore.h>
#include <Edje_Edit.h>

typedef struct appdata{
	Evas_Object* win_main;
	Evas_Object* layout_main;
	Evas* evas;
	void* char_db_object;
	Evas_Object* li;
	Evas_Object* status;
	Evas_Object* toolbar;
	Evas_Object* button;
	Evas_Object* bt1;
	Evas_Object* bt2;

	Evas_Object* edje;
	Evas_Object* mark1;
	Evas_Object* mark2;
	Evas_Object* mark3;
	GSList* spirit_list;
	GSList* mark_list;

	int id;
} appdata;

typedef struct message{
	int index;
	void *ad;
} message;

static void
win_del(void *data, Evas_Object *obj, void *event_info){
	libabs_appfwk_stop_loop();
}

static void clicked_cb(void* data, Evas_Object* obj, void* event_info){
	printf("clicked : name %s\n", evas_object_name_get(obj));
}

static 
Evas_Object* load_edj(Evas_Object* win_obj, char* filepath, char* groupname){
	Evas_Object* ret;
	int w, h;

	ret=elm_layout_add(win_obj);
	elm_layout_file_set(ret, filepath, groupname);
	evas_object_move(ret, 0, 0);
	elm_win_resize_object_add(win_obj, ret);
	evas_object_show(ret);
	return ret;
}

static void tb_cb_status(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_status\n");
}

static void tb_cb_spirit(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_spriti\n");
}

static void tb_cb_history(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_history\n");
}

static void ck_col(gpointer data, gpointer user_data){
	appdata* ad = (appdata*) user_data;
	Evas_Object* ins = (Evas_Object*) data;

	int x, y, w, h;
	int det_x, det_y, det_w, det_h;

	printf("\t\tname %s\n", evas_object_name_get(ins));
	evas_object_geometry_get(ins, &x, &y, &w, &h);
	printf("\t\t[x:%d y:%d w:%d h:%d]\n", x, y, w, h);

	int i=1;
	int n=g_slist_length(ad->spirit_list);
	for(;i<n;i++){
		Evas_Object* det = g_slist_nth_data(ad->spirit_list, i);
		evas_object_geometry_get(det, &det_x, &det_y, &det_w, &det_h);

		//AABB
		printf("\t\t\t[x:%d y:%d w:%d h:%d]\n", det_x, det_y, det_w, det_h);

		int px[2]; px[0] = det_x; px[1] = det_x+det_w;
		int py[2]; py[0] = det_y; py[1] = det_y+det_h;

		int q, e;
		for(q=0; q<2;q++){
			for(e=0; e<2; e++){
				if(x < px[q] && px[q] < x+w){
					if(y < py[e] && py[e] < y+h){
						if(strcmp(evas_object_name_get(ins), "zedoul")==0){
							edje_object_part_swallow(elm_layout_edje_get(ad->layout_main),"spirit1", det);
						} else if(strcmp(evas_object_name_get(ins), "option")==0){
							edje_object_part_swallow(elm_layout_edje_get(ad->layout_main),"spirit2", det);
						} else if(strcmp(evas_object_name_get(ins), "executer")==0){
							edje_object_part_swallow(elm_layout_edje_get(ad->layout_main),"spirit3", det);
						}
					}
				}
			}
		}

	}
}

static void sp_ok(void *data, Evas *e, Evas_Object *obj, void *event_info){
	int x, y, w, h;
	appdata* ad = (appdata*) data;
	Evas_Event_Mouse_Move *ins = (Evas_Event_Mouse_Move*) event_info;
	printf("[p:%d][x:%d, y:%d]\n", ins->buttons, ins->cur.output.x, ins->cur.output.y);


	if(ins->buttons == 1){
		evas_object_move(obj, ins->cur.output.x-30, ins->cur.output.y-30);
		evas_object_geometry_get(ad->mark1, &x, &y, &w, &h);
		printf("\t[x:%d y:%d w:%d h:%d]\n", x, y, w, h);

		g_slist_foreach(ad->mark_list, ck_col, ad);
	}
}

static Evas_Object* libui_create_spirit(void* data,int id, char* name){
	appdata* ad = (appdata*) data;
	Evas_Object* ic = elm_photo_add(ad->win_main);
	elm_photo_file_set(ic, "/usr/local/share/elementary/images/wood_01.jpg");
	evas_object_name_set(ic, name);

	ad->spirit_list = g_slist_append(ad->spirit_list, ic);

	return ic;
}

static Evas_Object* load_panel(void *data){
	appdata* ad = (appdata*) data;
	Evas_Object* ret = libabs_appfwk_load_edj(ad->win_main, "./status-list.edj", "state-list");
	return ret;
}

static libui_create_panel(void* data){
	appdata* ad = (appdata*) data;
	ad->edje = elm_layout_edje_get(ad->layout_main);
	ad->mark1 = edje_object_part_object_get(ad->edje,"mark1");
	ad->mark2 = edje_object_part_object_get(ad->edje,"mark2");
	ad->mark3 = edje_object_part_object_get(ad->edje,"mark3");
	evas_object_name_set(ad->mark1, "zedoul");
	evas_object_name_set(ad->mark2, "option");
	evas_object_name_set(ad->mark3, "executer");

	ad->mark_list = g_slist_append(ad->mark_list, ad->mark1);
	ad->mark_list = g_slist_append(ad->mark_list, ad->mark2);
	ad->mark_list = g_slist_append(ad->mark_list, ad->mark3);
}

int status_setting(void *data){
	appdata* ad = (appdata*) data;
	int x,y,w,h;

	//resize first
	evas_object_geometry_get(ad->win_main, &x, &y, &w, &h);

	evas_object_resize(ad->win_main,500, 500);
	ad->evas = evas_object_evas_get(ad->win_main);
	evas_object_show(ad->win_main);


	ad->layout_main = load_panel(ad); // libabs_appfwk_load_edj(ad->win_main, "./status-list.edj", "state-list");
	evas_object_resize(ad->layout_main,500, 500);

	Evas_Object* ls1 = libui_create_spirit(ad, 1, "i");
	Evas_Object* ls2 = libui_create_spirit(ad, 2, "n");
	Evas_Object* ls3 = libui_create_spirit(ad, 3, "r");



	libui_create_panel(ad);
	//	libui_panel_spirit(ls1);


	evas_object_resize(ls1, 50, 50);
	evas_object_move(ls1, 20, 50);
	evas_object_resize(ls2, 50, 50);
	evas_object_move(ls2, 20, 100);
	evas_object_resize(ls3, 50, 50);
	evas_object_move(ls3, 20, 150);

	evas_object_show(ls1);
	evas_object_show(ls2);
	evas_object_show(ls3);

	evas_object_show(ad->layout_main);
	//	Evas_Object* edje = elm_layout_edje_get(ad->layout_main);


	//	Evas_Object* spirit = edje_object_part_object_get(edje,"spirit");

	evas_object_event_callback_add(ls1, EVAS_CALLBACK_MOUSE_MOVE,  sp_ok, ad);
	evas_object_event_callback_add(ls2, EVAS_CALLBACK_MOUSE_MOVE,  sp_ok, ad);
	evas_object_event_callback_add(ls3, EVAS_CALLBACK_MOUSE_MOVE,  sp_ok, ad);


	evas_object_smart_callback_add(ls1, "clicked", clicked_cb, ad);
	evas_object_smart_callback_add(ls2, "clicked", clicked_cb, ad);
	evas_object_smart_callback_add(ls3, "clicked", clicked_cb, ad);

	return 0;
}

EAPI int 
elm_main(int argc, char** argv){
	appdata ad;

	libabs_appfwk_init();

	ad.win_main = libabs_appfwk_get_main_window("status");
	ad.spirit_list = g_slist_alloc();
	ad.mark_list = g_slist_alloc();

	evas_object_smart_callback_add(ad.win_main, "delete,request", win_del, NULL);

	Evas_Object* bg = elm_bg_add(ad.win_main);
	evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_win_resize_object_add(ad.win_main, bg);
	evas_object_show(bg);

	ecore_idler_add(status_setting, &ad);

	libabs_appfwk_start_loop();

	libabs_appfwk_quit();
}
ELM_MAIN()
